2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__
27 #define __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__
29 #include "../../ui/jucer_TestComponent.h"
30 #include "../../properties/jucer_FilePropertyComponent.h"
31 #include "../../properties/jucer_ComponentTextProperty.h"
32 #include "../../ui/jucer_MainWindow.h"
33 #include "jucer_ComponentUndoableAction.h"
36 //==============================================================================
39 class JucerComponentHandler
: public ComponentTypeHandler
42 //==============================================================================
43 JucerComponentHandler()
44 : ComponentTypeHandler ("Jucer Component", "xxx", typeid (TestComponent
), 300, 200)
47 //==============================================================================
48 Component
* createNewComponent (JucerDocument
* doc
)
50 return new TestComponent (doc
, 0, false);
53 const String
getXmlTagName() const throw() { return "JUCERCOMP"; }
55 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
57 TestComponent
* const tc
= dynamic_cast <TestComponent
*> (comp
);
59 XmlElement
* e
= ComponentTypeHandler::createXmlFor (comp
, layout
);
60 e
->setAttribute ("sourceFile", tc
->getFilename());
61 e
->setAttribute ("constructorParams", tc
->getConstructorParams());
66 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
68 TestComponent
* const tc
= dynamic_cast <TestComponent
*> (comp
);
70 if (! ComponentTypeHandler::restoreFromXml (xml
, comp
, layout
))
73 tc
->setFilename (xml
.getStringAttribute ("sourceFile", tc
->getFilename()));
74 tc
->setConstructorParams (xml
.getStringAttribute ("constructorParams"));
79 const String
getClassName (Component
* comp
) const
81 TestComponent
* const tc
= dynamic_cast <TestComponent
*> (comp
);
83 String jucerCompClassName
;
85 if (tc
->getDocument() != 0)
86 jucerCompClassName
= tc
->getDocument()->getClassName();
88 if (jucerCompClassName
.isEmpty())
89 jucerCompClassName
= "Component";
91 return jucerCompClassName
;
94 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
96 TestComponent
* const tc
= dynamic_cast <TestComponent
*> (component
);
98 ComponentTypeHandler::getEditableProperties (component
, document
, properties
);
100 properties
.add (new JucerCompFileProperty (tc
, document
));
101 properties
.add (new ConstructorParamsProperty (tc
, document
));
102 properties
.add (new JucerCompOpenDocProperty (tc
));
105 const String
getCreationParameters (Component
* component
)
107 TestComponent
* const tc
= dynamic_cast <TestComponent
*> (component
);
109 return tc
->getConstructorParams().trim();
111 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
113 ComponentTypeHandler::fillInCreationCode (code
, component
, memberVariableName
);
115 TestComponent
* const tc
= dynamic_cast <TestComponent
*> (component
);
117 code
.includeFilesH
.add (tc
->getFilename().replace (".cpp", ".h"));
120 //==============================================================================
121 class JucerCompFileChangeAction
: public ComponentUndoableAction
<TestComponent
>
124 JucerCompFileChangeAction (TestComponent
* const comp
, ComponentLayout
& layout
, const String
& newState_
)
125 : ComponentUndoableAction
<TestComponent
> (comp
, layout
),
128 oldState
= comp
->getFilename();
134 getComponent()->setFilename (newState
);
142 getComponent()->setFilename (oldState
);
147 String newState
, oldState
;
150 static void setJucerComponentFile (JucerDocument
& document
, TestComponent
* comp
, const String
& newFilename
)
155 document
.perform (new JucerCompFileChangeAction (comp
, *document
.getComponentLayout(), newFilename
),
156 "Change Jucer component file");
160 //==============================================================================
161 class JucerCompFileProperty
: public FilePropertyComponent
,
162 public ChangeListener
165 JucerCompFileProperty (TestComponent
* const component_
, JucerDocument
& document_
)
166 : FilePropertyComponent ("Jucer file", false, true),
167 component (component_
),
170 document
.addChangeListener (this);
173 ~JucerCompFileProperty()
175 document
.removeChangeListener (this);
178 //==============================================================================
179 void setFile (const File
& newFile
)
181 setJucerComponentFile (document
, component
,
182 newFile
.getRelativePathFrom (document
.getFile().getParentDirectory())
183 .replaceCharacter ('\\', '/'));
186 const File
getFile() const
188 return component
->findFile();
191 void changeListenerCallback (ChangeBroadcaster
*)
197 TestComponent
* const component
;
198 JucerDocument
& document
;
201 //==============================================================================
202 class JucerCompOpenDocProperty
: public ButtonPropertyComponent
205 JucerCompOpenDocProperty (TestComponent
* const component_
)
206 : ButtonPropertyComponent ("edit", false),
207 component (component_
)
213 MainWindow
* const mw
= findParentComponentOfClass ((MainWindow
*) 0);
217 mw
->openFile (component
->findFile());
220 const String
getButtonText() const
222 return "Open file for editing";
226 TestComponent
* const component
;
229 //==============================================================================
230 class ConstructorParamsProperty
: public ComponentTextProperty
<TestComponent
>
233 ConstructorParamsProperty (TestComponent
* comp
, JucerDocument
& document
)
234 : ComponentTextProperty
<TestComponent
> ("constructor params", 512, false, comp
, document
)
238 void setText (const String
& newText
)
240 document
.perform (new ConstructorParamChangeAction (component
, *document
.getComponentLayout(), newText
),
241 "Change Viewport content constructor params");
244 const String
getText() const
246 return component
->getConstructorParams();
252 class ConstructorParamChangeAction
: public ComponentUndoableAction
<TestComponent
>
255 ConstructorParamChangeAction (TestComponent
* const comp
, ComponentLayout
& layout
, const String
& newValue_
)
256 : ComponentUndoableAction
<TestComponent
> (comp
, layout
),
259 oldValue
= comp
->getConstructorParams();
265 getComponent()->setConstructorParams (newValue
);
267 layout
.getDocument()->refreshAllPropertyComps();
274 getComponent()->setConstructorParams (oldValue
);
276 layout
.getDocument()->refreshAllPropertyComps();
280 String newValue
, oldValue
;
286 #endif // __JUCER_JUCERCOMPONENTHANDLER_JUCEHEADER__